const sites = {
  'https://ao.sefan.me/': {
    searchInput: 'input[name="keyword"]',
    searchButton: 'button[type="submit"]'
  },
  'https://so5.xingqiu.icu/': {
    searchInput: 'input[name="keyword"]',
    searchButton: 'button[type="submit"]'
  },
  'https://hd.dobt.top/': {
    searchInput: 'input[name="keyword"]',
    searchButton: 'button[type="submit"]'
  },
  'https://t1.btapp.top/': {
    searchInput: 'input[name="keyword"]',
    searchButton: 'button[type="submit"]'
  },
  'https://bt2.foxso.top/': {
    searchInput: 'input[name="keyword"]',
    searchButton: 'button[type="submit"]'
  },
  'https://ciligou.de/': {
    searchInput: 'input[name="keyword"]',
    searchButton: 'button[type="submit"]'
  },
  'https://bt5.btsow.top/': {
    searchInput: 'input[name="keyword"]',
    searchButton: 'button[type="submit"]'
  }
};

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  if (request.action === 'search') {
    const keyword = request.keyword;
    Object.entries(sites).forEach(([url, selectors]) => {
      chrome.tabs.create({ url }, (tab) => {
        chrome.scripting.executeScript({
          target: { tabId: tab.id },
          func: performSearch,
          args: [selectors.searchInput, selectors.searchButton, keyword]
        });
      });
    });
  }
});

function performSearch(inputSelector, buttonSelector, keyword) {
  const input = document.querySelector(inputSelector);
  const button = document.querySelector(buttonSelector);
  
  if (input && button) {
    input.value = keyword;
    button.click();
  }
}
